//
//  ToolbarViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-5-12.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "ToolbarViewController.h"
#import "AppDelegate.h"
#import "ChatListViewController.h"
#import "PublishAirticleViewController.h"
#import "MyCommentsViewController.h"
#import "MoreViewController.h"
#import "ChatDAO.h"

#define Need_Push_Animation !ShowToolbarInWindow

@interface ToolbarViewController () {
    UIViewController *vc2,*vc3,*vc4,*vc5;
}

@end

@implementation ToolbarViewController
@synthesize currentIndex,currentViewController,homepageView,delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    currentIndex = 101;
    [self setToolbarItemSelectedWithitemTag:currentIndex];
    
    [self refreshMessages];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMessages) name:Notification_Chat_Recieved object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshMessages) name:Notification_Chat_Read object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (bool)hasThisViewController:(UIViewController*)vc {
    return NO;
    if (!vc) {
        return NO;
    }
    NSArray *arr = [[APP_DELEGATE navigationController] viewControllers];
    for (int i = 0; i < [arr count]; i++) {
        UIViewController *tVc = [arr objectAtIndex:i];
        if ([tVc isEqual:vc]) {
            return YES;
        }
    }
    return NO;
}


- (void)setToolbarItemSelectedWithitemTag:(int)itemTag {
    UIView *v1 = [self.view viewWithTag:201];
    UIView *v2 = [self.view viewWithTag:202];
    UIView *v3 = [self.view viewWithTag:203];
    UIView *v4 = [self.view viewWithTag:204];
    UIView *v5 = [self.view viewWithTag:205];
    
    //reset color
    [v1 setBackgroundColor:[UIColor clearColor]];
    [v2 setBackgroundColor:[UIColor clearColor]];
    [v3 setBackgroundColor:[UIColor clearColor]];
    [v4 setBackgroundColor:[UIColor clearColor]];
    [v5 setBackgroundColor:[UIColor clearColor]];
    
    UIView *sView = [self.view viewWithTag:itemTag + 100];
    [sView setBackgroundColor:GetColorWithRGB(141, 90, 144)];
}

- (IBAction)btnAction:(id)sender {
    if ([delegate respondsToSelector:@selector(btnClicked:)]) {
        [delegate btnClicked:sender];
        return;
    }
    
    UIButton *btn = (UIButton*)sender;
    if (btn.tag == currentIndex) {
        if (btn.tag == 101) {
            //处于主页，主页点击，滚动到顶部
            [[NSNotificationCenter defaultCenter] postNotificationName:Notification_homepage_clicked object:nil];
        }
        else if (btn.tag == 105) {
            //正处于侧滑页
            [[APP_DELEGATE sidePannelController] showCenterPanelAnimated:YES];
            currentIndex = 101;
        }
        return;
    }
    
    int oldIndex = currentIndex;
    currentIndex = btn.tag;
    
    if (btn.tag == 101) {
        [[APP_DELEGATE navigationController] popToRootViewControllerAnimated:Need_Push_Animation];
    }
    else if (btn.tag == 102) {
        if ([self hasThisViewController:vc2]) {
            [[APP_DELEGATE navigationController] popToViewController:vc2 animated:Need_Push_Animation];
        }
        else {
            ChatListViewController *cVc = [[ChatListViewController alloc] init];
            vc2 = cVc;
            [[APP_DELEGATE navigationController] pushViewController:cVc animated:Need_Push_Animation];
        }
        
    }
    else if (btn.tag == 103) {
        currentIndex = oldIndex;
        if ([self hasThisViewController:vc3]) {
            [[APP_DELEGATE navigationController] popToViewController:vc3 animated:Need_Push_Animation];
        }
        else {
            PublishAirticleViewController *wVc = [[PublishAirticleViewController alloc] init];
            vc3 = wVc;
            [[APP_DELEGATE navigationController] pushViewController:wVc animated:Need_Push_Animation];
        }
    }
    else if (btn.tag == 104) {
        if ([self hasThisViewController:vc4]) {
            [[APP_DELEGATE navigationController] popToViewController:vc4 animated:Need_Push_Animation];
        }
        else {
            MyCommentsViewController *mVc = [[MyCommentsViewController alloc] init];
            vc4 = mVc;
            [[APP_DELEGATE navigationController] pushViewController:mVc animated:Need_Push_Animation];
        }
    }
    else if (btn.tag == 105) {
        [[APP_DELEGATE sidePannelController] showLeftPanelAnimated:YES];
        return;
//        if ([self hasThisViewController:vc5]) {
//            [[APP_DELEGATE navigationController] popToViewController:vc5 animated:Need_Push_Animation];
//        }
//        else {
//            MoreViewController *mVc = [[MoreViewController alloc] init];
//            vc5 = mVc;
//            [[APP_DELEGATE navigationController] pushViewController:mVc animated:Need_Push_Animation];
//        }
        
    }
    return;
    [self setToolbarItemSelectedWithitemTag:currentIndex];
    
}

- (void)refreshMessages {
    NSMutableArray *unreadMessages= [[ChatDAO shareChatDAO] getAllUnreadMessages];
    if ([unreadMessages count] < 1) {
        [viewBadge setHidden:YES];
        labelBadge.text = @"";
        return;
    }
    int badgeNum = [unreadMessages count];
    if (badgeNum > 99) {
        badgeNum = 99;
    }
    [viewBadge setHidden:NO];
    labelBadge.text = [NSString stringWithFormat:@"%d",badgeNum];
}


@end
